package chances.system.operator.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import chances.system.json.JsonResponse;
import chances.system.log.entity.ActionLog;
import chances.system.operator.entity.Oplog;
import chances.system.operator.entity.Organization;
import chances.system.operator.repo.OrganizationRepository;
import chances.webui.model.Tree;
import chances.webui.model.TreeNode;

@RestController
public class OrganizationController {

	ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
	 
	@Autowired
	OrganizationRepository organizationRepository;
	
	@ActionLog(operateType=Oplog.OP_ADD, objName="organization", desc="添加组织'{name}'")
	@RequestMapping(path = "/sys/org/save", method = RequestMethod.POST)
	public Organization saveRole(HttpServletRequest request, @RequestBody OrganizationModel model) {
		Organization organization = this.organizationRepository.save(model.getOrganization());
		request.setAttribute("organization", model.getOrganization());
		return organization;
	}
	
	@ActionLog(operateType=Oplog.OP_DELETE, objName="organization", desc="删除组织'{name}'")
	@RequestMapping(value = "/sys/org/delete/{id}", method = RequestMethod.GET)
	public @ResponseBody JsonResponse delete(HttpServletRequest request, @PathVariable("id") Long id, Model model) {
		JsonResponse response = new JsonResponse();
		Organization organization = organizationRepository.findOne(id);
		request.setAttribute("organization", organization);
		organizationRepository.deleteById(id);
		return response;
	}
	
	@RequestMapping(path = "/sys/org/tree", method = RequestMethod.GET)
	public List<Map<String, Object>> getOrganizationTree(HttpServletRequest httpServletRequest) {
		List<Organization> organizations = organizationRepository.findAvaliableOrganization();
		List<TreeNode<Organization>> nodes = organizations.stream().map(s -> {
			return createOrganizationNode(s);
		}).collect(Collectors.toList());
		Tree<Organization> organizationTree = new Tree<Organization>(nodes);

		List<Map<String, Object>> map = createNodeListMap(organizationTree.getRootNodes());
		return map;
	}
	
	public TreeNode<Organization> createOrganizationNode(Organization organization) {
		TreeNode<Organization> node = new TreeNode<Organization>();
		node.setEntry(organization);
		node.setTitle(organization.getName());
		node.setId(organization.getOrgId().toString());
		if(organization.getParentId() != null){
			node.setParentId(organization.getParentId().toString());
		}
		return node;
	}
	
	public List<Map<String, Object>> createNodeListMap(List<TreeNode<Organization>> childrens){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (!childrens.isEmpty()) {
			for (TreeNode<Organization> children : childrens) {
				list.add(createNodeMap(children));
			}
		}
		return list;
	}

	public Map<String, Object> createNodeMap(TreeNode<Organization> node){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", node.getId());
		map.put("text", node.getTitle());
		map.put("children", createNodeListMap(node.getChildren()));
		return map;
	}	
}
